package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.MongoDbConstant;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.SignalHis;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.SimpleMongoService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Author: wulongbo
 * @Date : 2021/4/9 11:19
 * @Version 1.0
 */
@Component
public class EmqSignalUploadStrategy implements EmqStrategy {


    @Autowired
    private SimpleMongoService simpleMongoService;

    @Autowired
    private DeviceService deviceService;

    // 上报信号等设备信息
    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSON.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (!OptionalBean.ofNullable(imei).isPresent()) {
                return "json解析错误";
            }
            Integer wifi = jsonObject.getInteger("wifi");
            if (!OptionalBean.ofNullable(wifi).isPresent()) {
                return "json解析错误";
            }

            Integer gps = jsonObject.getInteger("gps");
            if (!OptionalBean.ofNullable(gps).isPresent()) {
                return "json解析错误";
            }
            Integer fg = jsonObject.getInteger("fg");
            if (!OptionalBean.ofNullable(fg).isPresent()) {
                return "json解析错误";
            }
            if (fg.compareTo(0) < 0) {
                if (fg.compareTo(-25) >= 0) {
                    fg = Integer.valueOf(4);
                } else if (fg.compareTo(-40) >= 0) {
                    fg = Integer.valueOf(3);
                } else if (fg.compareTo(-50) >= 0) {
                    fg = Integer.valueOf(2);
                } else if (fg.compareTo(-75) >= 0) {
                    fg = Integer.valueOf(1);
                } else if (fg.compareTo(-90) >= 0) {
                    fg = Integer.valueOf(0);
                }
            }

            Integer bluetooth = jsonObject.getInteger("bluetooth");
            if (!OptionalBean.ofNullable(bluetooth).isPresent()) {
                return "json解析错误";
            }
            // TODO
            SignalHis signalHis = new SignalHis();
            signalHis.setImei(imei);
            signalHis.setWifi(wifi);
            signalHis.setGps(gps);
            signalHis.setFg(fg);
            signalHis.setBluetooth(bluetooth);
            signalHis.setCreateTime(System.currentTimeMillis());
            signalHis.setCreatedBy("emq");
            simpleMongoService.insert(signalHis, MongoDbConstant.SIGN_HIS);


            Device device = new Device();
            device.setImei(imei);
            device.setWifi(wifi);
            device.setGps(gps);
            device.setFg(fg);
            device.setBluetooth(bluetooth);
            deviceService.updateByImeiSelective(device);
        } catch (Exception e) {
            return "上报信号等设备信息失败！";
        }
        return "上报信号等设备信息成功！";
    }
}
